package com.xiangying.controller;

import java.io.IOException;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xiangying.entity.User;
import com.xiangying.entity.common.Pageable;
import com.xiangying.entity.common.Pagination;
import com.xiangying.entity.common.Result;
import com.xiangying.entity.common.ResultEnum;
import com.xiangying.exception.BusinessException;
import com.xiangying.service.demo.UserService;
import com.xiangying.utils.RedisUtils;
import com.xiangying.utils.ResultUtils;

@RestController
public class DemoController{
	
	protected final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private UserService userService;
	
	/**
	 * 保存用户
	 * @param user
	 * @return	
	 */
	@PostMapping("/demo/save")
	public Result<String> save(@Valid User user, BindingResult bindingResult){
		if (bindingResult.hasErrors()) {
			return ResultUtils.error(ResultEnum.FAILED.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
		
		userService.insertSelective(user);
		return ResultUtils.success();
	}
	
	/**
	 * 更新用户
	 * @param user
	 * @return
	 */
	@PostMapping("/demo/update")
	public Result<String> update(User user){
		userService.updateByPrimaryKeySelective(user);
		return ResultUtils.success();
	}
	
	/**
	 * 删除用户
	 * @param user
	 * @return
	 */
	@GetMapping("/demo/delete")
	public Result<String> delete(Long id){
		userService.deleteByPrimaryKey(id);
		return ResultUtils.success();
	}
	
	/**
	 * 获取信息
	 * @param name
	 * @param age
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@GetMapping("/demo/get/{id}")
	public Result<User> getUser(@PathVariable("id") Long id){
		return ResultUtils.success(userService.selectByPrimaryKey(id));
	}
	
	/**
	 * 获取分页数据
	 * @param pageable
	 * @return
	 */
	@GetMapping("/demo/page")
	public Pagination<User> getUserPage(Pageable pageable){
		return userService.findPage(pageable);
	}
	
	/**
	 * 测试日志输出
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@GetMapping("/demo/logs/{isThrow}")
    public Result<String> logs(@PathVariable("isThrow") Boolean isThrow){ 
        logger.debug("DEBUG TEST 这个地方输出DEBUG级别的日志");  
        logger.info("INFO TEST 这个地方输出INFO级别的日志");  
        logger.warn("WARN TEST 这个地方输出WARN级别的日志");
        logger.error("ERROR TEST 这个地方输出ERROR级别的日志"); 
        // 如果需要抛出异常，如下
        if(isThrow){
        	throw new BusinessException(ResultEnum.DATABASE_ERROR);
        }
        return ResultUtils.success();
    }  
	
	@GetMapping("/demo/redis/test")
	public Result<String> redisTest(String key, String value){
		logger.info("set Redis Key =" + key);
		RedisUtils.setRedis(key, value);
		logger.info("get RedisValue = " + RedisUtils.getRedis(key));
		return ResultUtils.success();
	}
}
